<?php

namespace App\Models;

class StoreEvaluates extends Base
{
    protected $table = 'store_evaluates';

    const TYPE_1 = 1;
    const TYPE_2 = 2;
    const TYPE_3 = 3;
    const TYPE_TEXT = [
        self::TYPE_1 => '<span class="el-tag  el-tag--danger">好评</span>',
        self::TYPE_2 => '<span class="el-tag">中评</span>',
        self::TYPE_3 => '<span class="el-tag">差评</span>',
    ];

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">禁止显示</span>',
    ];


    protected $fillable = [
        'id','user_id','user_name','user_avatar','store_id','store_name','content','speed','quality','attitude',
        'average_star','type','status','created_at','updated_at','business_name'
    ];

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['type']) && $params['type']) {
                    $query ->where('type',$params['type']);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->where('status','<>',-1)
            ->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
            'type'   => self::TYPE_TEXT,
        ]);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = '', $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        $params['status'] = 1;
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['type']) && $params['type']) {
                    $query ->where('type',$params['type']);
                }
                if(isset($params['store_id']) && $params['store_id']) {
                    $query ->where('store_id',$params['store_id']);
                }
            })->where('status','<>',-1)
            ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        return $lists;
    }

    /**
     * 获取列表-接口
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function apiList($params = [], $limit = 10, $fields = ['*'],$sort=''){
        $params['status'] = 1;
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['type']) && $params['type']) {
                    $query ->where('type',$params['type']);
                }
            })->where('status','<>',-1)
            ->orderBy($sort,'desc')->limit($limit)->get();
        return $lists;
    }

    /**
     * 查询评价店铺条数
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function counts($params = []){
        return $this->where($params)->where('status','<>',-1)->count();
    }

    /**
     * 查询某字段总和
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function sums($params = [], $sum = 'average_min'){
        return $this->where($params)->where('status','<>',-1)->sum($sum);
    }


}
